import yaml
import sys
import os


def output_url(urls):
    if type(urls) == type([1,2]):
        for url in urls:
            print(url)
    else:
        print(urls)

# Query all package and versions
def query_package_version(root_dir, package):
    path_t = package
    root_dir = root_dir + package + '\\'
    try:
        r_config = open(root_dir + "config.yml", 'r')
        config_r = yaml.load(r_config)
    except:
        config_r = {}
        avo = {}
        for xc in os.listdir(root_dir):
            obj = {}
            obj['folder'] = xc
            avo[xc] = obj
        config_r['versions'] = avo

    for key in config_r['versions']:
        # load subversion
        print("Package:",package+"/"+key)
        config_f = root_dir + config_r['versions'][key]['folder'] + '\\conandata.yml'
        if os.path.exists(config_f):
            version_dir = open(config_f, 'r')
            version_data = yaml.load(version_dir)

            sources = version_data['sources']
            sources_info = sources[key]

            varray = []
            if type(sources_info) == type([1,2]):
                pass
            else:
                varray.append(sources_info)
            
            for sources_info in varray:
                if type(sources_info['url']) == type("a") or type(sources_info['url']) == type([1,2]):
                    print("url:")
                    output_url(sources_info['url'])
                else:
                    for tk in sources_info['url']:
                        vt = sources_info['url'][tk]
                        if 'url' in vt:
                            print("url:")
                            print(vt['url'])



import sys

if len(sys.argv) != 2:
    print("python parser.py index_idr")
    sys.exit(-1)
     

#root_dir = 'D:\\temp\\test\\imlaji-conan-center-index-master\\conan-center-index\\recipes\\'
root_dir = sys.argv[1]

for file in os.listdir(root_dir):
    #print("======================>>>>>>Process package:", file)
    package = file
    query_package_version(root_dir, package)
